/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.management.user.web;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tompai.common.constants.ReturnStatusConstant;
import com.tompai.common.model.SysRole;
import com.tompai.management.user.service.SysRolePermissionRelationService;
import com.tompai.management.user.service.SysRoleService;
import com.tompai.management.user.web.vo.SysRoleInfoVo;
import com.tompai.management.user.web.vo.SysRolePermissionRelationQueryVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 用户信息控制层
 * 
 * @author liinux@qq.com
 * @date 2019/10/5
 */
@Controller
@Api(tags = "角色信息管理{用户管理:/user/manager}")
@RequestMapping("/user/role")
public class UserRoleController {

	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysRolePermissionRelationService sysRolePermissionRelationService;

	@Value("${admin.role.id}")
	private String adminRoleId;

	@ApiOperation(value = "获取角色列表")
	@GetMapping
	public String sysRoleInfo() {
		return "user/sys-role-info";
	}

	@ApiOperation(value = "查询指定角色信息")
	@GetMapping("/get-sys-role-info/{id}")
	@ResponseBody
	public SysRole getSysRoleInfo(@PathVariable("id") @ApiParam(name = "id", value = "主键id") String id) {
		return sysRoleService.getSysRoleInfo(id);
	}

	@ApiOperation(value = "查询所有角色信息")
	@GetMapping("/get-all-sys-role-info")
	@ResponseBody
	public List<SysRole> getAllSysRoleInfo() {
		return sysRoleService.selectSysRoleInfo(new SysRole());
	}

	@ApiOperation(value = "分页查询角色信息")
	@GetMapping("/select-sys-role-info")
	@ResponseBody
	public SysRoleInfoVo selectSysRoleInfo(SysRole sysRole, int page, int limit) {
		PageHelper.startPage(page, limit);
		List<SysRole> sysRoleInfos = sysRoleService.selectSysRoleInfo(sysRole);
		// 过滤超级管理员角色
		sysRoleInfos = sysRoleInfos.stream().filter(sr -> !sr.getId().equals(adminRoleId)).collect(Collectors.toList());
		PageInfo pageInfo1 = new PageInfo(sysRoleInfos);
		SysRoleInfoVo sysRoleInfoVo = new SysRoleInfoVo();
		sysRoleInfoVo.setCode("0");
		sysRoleInfoVo.setCount(Long.valueOf(pageInfo1.getTotal()).intValue());
		sysRoleInfoVo.setData(sysRoleInfos);
		return sysRoleInfoVo;
	}

	@ApiOperation(value = "添加/编辑用户角色信息")
	@PostMapping("/add-or-update-sys-role-info")
	@ResponseBody
	public ReturnStatusConstant addOrUpdateSysRoleInfo(@RequestBody SysRole sysRole) {
		ReturnStatusConstant returnCode = new ReturnStatusConstant();
		try {
			if (StringUtils.hasText(sysRole.getId())) {
				sysRole.beforeUpdate();
				sysRoleService.update(sysRole);
			} else {
				sysRole.beforeInsert();
				sysRoleService.insert(sysRole);
			}
			returnCode.setCode(ReturnStatusConstant.SUCCESS);
			returnCode.setMessage(ReturnStatusConstant.SUCCESS_MESSAGE);
		} catch (Exception e) {
			returnCode.setCode(ReturnStatusConstant.FAIL);
			returnCode.setMessage(ReturnStatusConstant.SUCCESS_FAIL);
		}
		return returnCode;
	}

	@ApiOperation(value = "保存角色权限信息")
	@PostMapping("/add-role-permission-relation")
	@ResponseBody
	public ReturnStatusConstant addRolePerMissionRelation(
			@RequestBody SysRolePermissionRelationQueryVo sysRolePermissionRelationQueryVo) {
		ReturnStatusConstant returnCode = new ReturnStatusConstant();
		try {
			sysRolePermissionRelationService.addRolePerMissionRelation(sysRolePermissionRelationQueryVo);
			returnCode.setCode(ReturnStatusConstant.SUCCESS);
			returnCode.setMessage(ReturnStatusConstant.SUCCESS_MESSAGE);
		} catch (Exception e) {
			returnCode.setCode(ReturnStatusConstant.FAIL);
			returnCode.setMessage(ReturnStatusConstant.SUCCESS_FAIL);
		}
		return returnCode;
	}

	@ApiOperation(value = "删除用户角色信息")
	@PostMapping("/delete-sys-role-info")
	@ResponseBody
	public ReturnStatusConstant deleteSysRoleInfo(
			@RequestParam("id") @ApiParam(name = "id", value = "主键id") String id) {
		ReturnStatusConstant returnCode = new ReturnStatusConstant();
		try {
			sysRoleService.deleteByPrimaryKey(id);//
			returnCode.setCode(ReturnStatusConstant.SUCCESS);
			returnCode.setMessage(ReturnStatusConstant.SUCCESS_MESSAGE);
		} catch (Exception e) {
			returnCode.setCode(ReturnStatusConstant.FAIL);
			returnCode.setMessage(ReturnStatusConstant.SUCCESS_FAIL);
		}
		return returnCode;
	}
}
